﻿using StarPizza.Model.BaseModel;
using StarPizza.Model.Core;
using StarPizza.Model.User;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.Core
{
    class DoughLister : IngredientLister<Dough, DoughVM>
    {
        public DoughLister(IIngredientResource ingredientResource, IUserModeManager userModeManager)
            : base(ingredientResource, userModeManager)
        {
            SetupIngredients();
            ingredientResource.DoughStorageChanged += IngredientStorageChanged;
            ingredientResource.DoughCreated += ingredientResource_DoughCreated;
            SetupSelectedDough();
        }
        void ingredientResource_DoughCreated(Dough dough)
        {
            RegisterIngredient(ingredient: dough);
        }
        protected override DoughVM CreateIngredientVM(Dough dough)
        {
            return new DoughVM(dough);
        }
        protected override IEnumerable<Dough> GetSpecificIngredients()
        {
            return ingredientResource.GetDoughs();
        }
        void SetupSelectedDough()
        {
            var doughsWithSize32 = IngredientVMs.Where(dough => dough.Size == 32);
            if (doughsWithSize32.Any())
            {
                SelectedIngredientVM = doughsWithSize32.First();
            }
            else
            {
                //TODO select first available
                if (IngredientVMs.Any())
                {
                    SelectedIngredientVM = IngredientVMs.First();
                }
            }
        }
    }
}
